const path = require("path");

const VueLoaderPlugin = require("vue-loader/lib/plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CleanWebpackPlugin = require("clean-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCssPlugin = require("optimize-css-assets-webpack-plugin");

const AutoprefixerPlugin = require("autoprefixer");
const postCssPluginsOptions = [
    AutoprefixerPlugin({
        browsers: ["last 5 versions"]
    })
];

module.exports = {
    mode: "production",
    entry: {
        manifest: path.join(__dirname, "../src/main.js")
    },
    output: {
        path: path.join(__dirname, "../dist/"),
        filename: "static/[name].[chunkhash:8].js",
        publicPath: ""
    },
    module: {
        rules: [
            {
                test: /\.vue$/,
                loader: "vue-loader"
            },
            {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                loader: "babel-loader"
            },
            {
                test: /\.css$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    {
                        loader: "css-loader",
                        options: {
                            importLoaders: 1
                        }
                    },
                    {
                        loader: "postcss-loader",
                        options: {
                            ident: "postcss",
                            plugins: postCssPluginsOptions
                        }
                    }
                ]
            },
            {
                test: /\.scss$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    "css-loader",
                    {
                        loader: "postcss-loader",
                        options: {
                            sourceMap: true,
                            ident: "postcss",
                            plugins: postCssPluginsOptions
                        }
                    },
                    "sass-loader"
                ]
            },
            {
                test: /\.sass$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    "css-loader",
                    {
                        loader: "postcss-loader",
                        options: {
                            sourceMap: true,
                            ident: "postcss",
                            plugins: postCssPluginsOptions
                        }
                    },
                    {
                        loader: "sass-loader",
                        options: {
                            indentedSyntax: true
                        }
                    }
                ]
            },
            {
                test: /\.(gif|jpg|jpeg|png|svg)$/i,
                loader: "url-loader",
                options: {
                    // 当文件大小小于 limit byte 时会把图片转换为 base64 编码，否则返回普通的图片
                    limit: 8192,
                    name: "images/[hash:8].[ext]"
                }
            },
            {
                test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
                loader: "url-loader",
                options: {
                    limit: 10000,
                    name: "fonts/[hash:8].[ext]"
                }
            }
        ]
    },
    plugins: [
        new VueLoaderPlugin(),
        new HtmlWebpackPlugin({
            filename: "index.html",
            template: "configs/template/index.html",
            inject: "body"
        }),
        new MiniCssExtractPlugin({
            filename: "static/manifest.[hash:8].css",
            chunkFilename: "static/[id].[hash:8].css"
        }),
        new OptimizeCssPlugin({
            cssProcessor: require("cssnano"),
            cssProcessorOptions: {
                discardComments: {
                    removeAll: true
                }
            },
            canPrint: true
        }),
        new CleanWebpackPlugin(["*"], {
            root: path.join(__dirname, "../dist"),
            verbose: true,
            dry: false
        })
    ],
    resolve: {
        alias: {
            "vue": path.join(__dirname, "../node_modules/vue/dist/vue.min.js")
        }
    }
};
